package com.sjd.mall.service.order.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.mall.dao.SjdOrderDao;
import com.sjd.mall.domain.SjdOrderDO;
import com.sjd.mall.domain.SjdOrderItemsDO;
import com.sjd.mall.domain.dto.base.BaseGoodsVO;
import com.sjd.mall.domain.dto.order.WebCourierEntryRequestDTO;
import com.sjd.mall.domain.dto.order.WebOrderQryRequestDTO;
import com.sjd.mall.domain.dto.order.WebOrderQryResponseDTO;
import com.sjd.mall.domain.dto.order.WebOrderStatsResponseDTO;
import com.sjd.mall.enums.GoodsClass;
import com.sjd.mall.enums.OrderStatus;
import com.sjd.mall.mapper.SjdOrderItemsMapper;
import com.sjd.mall.mapper.SjdOrderMapper;
import com.sjd.mall.service.order.WebOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * WebOrder
 *
 * @author seer
 * @version 1.0
 * @date 2022/1/20 15:46
 */
@Service("webOrderService")
@Slf4j
public class WebOrderServiceImpl implements WebOrderService {

    @Autowired
    private SjdOrderMapper orderMapper;

    @Autowired
    private SjdOrderItemsMapper orderItemsMapper;

    @Autowired
    private SjdOrderDao orderDao;

    /**
     * 快递录入
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public AjaxResult courierEntry(WebCourierEntryRequestDTO requestDTO) {
        SjdOrderDO orderDO = orderMapper.getByOrderNo(requestDTO.getOrderNo());
        Assert.notNull(orderDO, "订单不存在 " + requestDTO.getOrderNo());
        if (!OrderStatus.PAY_SUCCESS.value.equals(orderDO.getOrderStatus())) {
            return AjaxResult.error("订单状态错误，不允许发货");
        }
        SjdOrderDO update = new SjdOrderDO();
        update.setOrderStatus(OrderStatus.DELIVER_SUCCESS.value);
        update.setOrderStatusTime(LocalDateTime.now());
        update.setDeliverTime(requestDTO.getDeliverTime());
        update.setCourierCompany(requestDTO.getCourierCompany());
        update.setCourierNo(requestDTO.getCourierNo());
        update.setOrderNo(requestDTO.getOrderNo());
        orderMapper.updateSelectiveByOrderNo(update);
        return AjaxResult.success();
    }

    /**
     * 订单查询
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public List<WebOrderQryResponseDTO> qry(WebOrderQryRequestDTO requestDTO) {
        List<WebOrderQryResponseDTO> responseList = orderDao.listOrder(requestDTO);
        for (WebOrderQryResponseDTO resp : responseList) {
            List<SjdOrderItemsDO> itemsList = orderItemsMapper.listByOrderNo(resp.getOrderNo());
            List<BaseGoodsVO> respItems = itemsList.stream().map(items -> {
                BaseGoodsVO goodsVO = new BaseGoodsVO();
                goodsVO.setId(items.getId());
                goodsVO.setGoodsId(items.getGoodsId());
                goodsVO.setGoodsQuantity(items.getGoodsQuantity());
                goodsVO.setGoodsName(items.getGoodsName());
                goodsVO.setGoodsPicture(items.getGoodsPicture());
                goodsVO.setGoodsPrice(items.getGoodsPrice());
                goodsVO.setGoodsAmount(items.getGoodsAmount());
                goodsVO.setDiscountAmount(items.getDiscountAmount());
                goodsVO.setPayableAmount(items.getPayableAmount());
                return goodsVO;
            }).collect(Collectors.toList());
            resp.setGoodsItems(respItems);
        }
        return responseList;
    }

    /**
     * 当日订单统计
     *
     * @return
     */
    @Override
    public WebOrderStatsResponseDTO stats() {
        LocalDateTime startTime = LocalDateTimeUtil.beginOfDay(LocalDateTime.now());
        LocalDateTime endTime = LocalDateTimeUtil.endOfDay(LocalDateTime.now());
        LocalDate statsDate = LocalDateTimeUtil.ofDate(LocalDateTime.now());
        List<SjdOrderDO> goodsList = orderMapper.listPositive(GoodsClass.GOODS, startTime, endTime);
        BigDecimal goodsAmount = goodsList.stream().map(SjdOrderDO::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<SjdOrderDO> courseList = orderMapper.listPositive(GoodsClass.COURSE, startTime, endTime);
        BigDecimal courseAmount = courseList.stream().map(SjdOrderDO::getOrderAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        WebOrderStatsResponseDTO response = new WebOrderStatsResponseDTO();
        response.setGoodsTotalOrderAmount(goodsAmount);
        response.setGoodsTotalOrderCount(goodsList.size());
        response.setCourseTotalOrderAmount(courseAmount);
        response.setCourseTotalOrderCount(courseList.size());
        response.setFunTotalOrderAmount(BigDecimal.ZERO);
        response.setFunTotalOrderCount(0);
        response.setStatsDate(statsDate);
        return response;
    }
}
